package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * Title:
 * Author: fusu
 * DateTime:  2023/3/24
 * Description:
 **/
@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierMapper supplierMapper;

    /**
     * @return java.util.List<com.goods.common.model.business.Supplier>
     * @Author: fusu
     * @Description: 物资来源分类列表展示+查询
     * @DateTime:15:26 2023/3/24
     * @params: [pageNum, pageSize, supplier]
     */
    @Override
    public PageVO findSupplierList(Integer pageNum, Integer pageSize, Supplier supplier) {
        PageHelper.startPage(pageNum, pageSize);
        Example example = new Example(Supplier.class);
        String name = supplier.getName();//具体地址
        String address = supplier.getAddress();//省市区
        String contact = supplier.getContact();//联系人
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(name)) {
            criteria.orEqualTo("name", name);
        }
        if (!StringUtils.isEmpty(address)) {
            criteria.orEqualTo("address", address);
        }
        if (!StringUtils.isEmpty(contact)) {
            criteria.orEqualTo("contact", contact);
        }
        List<Supplier> suppliers = supplierMapper.selectByExample(example);
        PageInfo<Supplier> info = new PageInfo<>(suppliers);
        return new PageVO<>(info.getTotal(), suppliers);
    }

    /**
     * @return void
     * @Author: fusu
     * @Description: 添加物资来源
     * @DateTime:16:06 2023/3/24
     * @params: [supplier]
     */
    @Override
    public void addSupplier(Supplier supplier) {
        supplier.setCreateTime(new Date());
        supplier.setModifiedTime(new Date());
        supplierMapper.insert(supplier);
    }

    /**
     * @return com.goods.common.model.business.Supplier
     * @Author: fusu
     * @Description: 根据id查询物资来源
     * @DateTime:16:11 2023/3/24
     * @params: [supplierId]
     */
    @Override
    public Supplier getBySupplierId(Long supplierId) {
        return supplierMapper.selectByPrimaryKey(supplierId);
    }

    /**
     * @return void
     * @Author: fusu
     * @Description: 根据id删除
     * @DateTime:16:13 2023/3/24
     * @params: [supplierId]
     */
    @Override
    public void deleteBySupplierId(Long supplierId) {
        supplierMapper.deleteByPrimaryKey(supplierId);
    }

    /**
     * @return void
     * @Author: fusu
     * @Description: 根据id更新
     * @DateTime:16:17 2023/3/24
     * @params: [supplier]
     */
    @Override
    public void updateBySupplierId(Supplier supplier) {
        supplierMapper.updateByPrimaryKey(supplier);
    }

    /**
     * @return java.util.List<com.goods.common.model.business.Supplier>
     * @Author: fusu
     * @Description: 获取所有
     * @DateTime:14:43 2023/3/25
     * @params: []
     */
    @Override
    public List<Supplier> findAll() {
        return supplierMapper.selectAll();
    }
}
